<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\common\model\printmanager\Org;
use app\common\model\printmanager\Production;
use app\common\model\printmanager\Shipping;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 印刷合作机构接口
 */
class Printingorg extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /*
     * 印刷机构合作申请
     */
    public function orgApply(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $allow_apply = Org::where($where)->field('`status`')->find();
        if($allow_apply){
            if($allow_apply['status'] == 0){
                $this->error('申请等待审核，请勿重复提交');
            }

            if($allow_apply['status'] == 1){
                $this->error('申请已审核通过，请勿重复提交');
            }

            if($allow_apply['status'] == 2){
                $allow_apply->delete();
            }
        }

        $data = $this->request->post();
        $data['user_id'] = $user_id;
        $data['org_code'] = Random::alnum(8);
        unset($data['token']);
        if(isset($data['share_from'])) unset($data['share_from']);
        $is_success = Org::create($data);
        $this->success('提交成功', $is_success);
    }

    /*
     * 印刷机构信息
     */
    public function getOrgInfo(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $where['status'] = array('in', '0, 1');
        $field = 'id, org_name, org_code, province_id, city_id, county_id, address, telephone,
            linkname, linktelephone, qq, createtime, user_id, status';
        $org_info = Org::where($where)->field($field)->find();
        $this->success('印刷机构信息', $org_info);
    }

    /*
     * 添加我的印刷完成和物流
     */
    public function doneProduction(){
        $user_id = $this->auth->id;
        $where['user_id'] = $user_id;
        // $data = $this->request->post();
        // $data['user_id'] = $user_id;
        // $data['prin_id'] = $data['prin_id'] ? $data['prin_id'] : $org_info['id'];
        // $data['prin_no'] = $data['prin_no'] ? $data['prin_no'] : $org_info['org_code'];
        // $data['org_name'] = $data['org_name'] ? $data['org_name'] : $org_info['org_name'];
        // $data['prod_no'] = $data['prod_no'] ? $data['prod_no'] : Random::alnum(15);
        $data['finishtime'] = strtotime($data['finishtime']);
        unset($data['token']);

        $is_success = Production::create($data);
        $this->success('提交成功', $is_success);
    }


    /*
     * 查询我的印刷
     */
    public function getProduction(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;

        $org =  Org::where($where)->find();
        if(!$org || $org->status != 1){
            $this->error("非印刷机构");
        }

        $order = 'id DESC';
        $productionList = Production::where('org_id', $org->id)->order($order)->paginate()->each(function($item){
            $item->needs_info = $item->needs_info;
        });

        $this->success('我的印刷', $productionList);
    }
    
}
